class Solution {
public:
    int dp[1005][1005];
    int longestPalindromeSubseq(string s) {
        int ans = 1;
        for (int i = 0; i < s.size(); i++) dp[i][i] = 1;
        for (int i = 2; i <= s.size(); i++)
        {
            int l = 0, r = i - 1;
            while (r < s.size())
            {
                if (s[l] == s[r])
                {
                    if (i == 2) dp[l][r] = 2;
                    else dp[l][r] = dp[l + 1][r - 1] + 2;
                }
                else dp[l][r] = max(dp[l + 1][r], dp[l][r - 1]);
                ans = max(ans, dp[l][r]);
                l += 1; r += 1;
            }
        }
        return ans;
    }
};